<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <a-radio-group default-value="a" v-model="model.type" button-style="solid">
                <a-radio-button value="1">定金合同</a-radio-button>
                <a-radio-button value="2">正式合同</a-radio-button>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="合同文件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="depositoryMaterialNum">
              <a-upload
                name="file"
                :fileList="fileList"
                id="upload"
                @change="handleChange"
                :beforeUpload="doBeforeUpload"
              >
                <a-button> <a-icon type="upload" /> Click to Upload </a-button>
              </a-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'GhContractFileForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        fileList:[],
        confirmLoading: false,
        validatorRules: {
        },
        uploadAction: window._CONFIG['domianURL'] + "/sys/oss/file/upload",
        action: window._CONFIG['domianURL'] + "/sys/oss/file/upload",
        url: {
          add: "/ghContractFile/add",
          edit: "/ghContractFile/edit",
          queryById: "/ghContractFile/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      doBeforeUpload(file){
        console.log("======="+Object.keys(file))
        console.log("=======",file)
        this.uploadGoOn=true
        const isLt10M = file.size / 1024 / 1024 < 20
        if (!isLt10M) {
          this.$message.error('上传文件大小不能超过 20MB!')
          this.uploadGoOn=false
          return false;
        }
        return true
      },
      handleChange(file,fileLists){
        console.log('----',file,fileLists)
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>